﻿using _1._8._2015_KidChores.Data;
using _1._8._2015_KidChores.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity.Migrations;
using _1._8._2015_KidChores.Data.Models;

namespace _1._8._2015_KidChores.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Kids()
        {
            KidChoreDbContext db = new KidChoreDbContext();
            KidsViewModel model = new KidsViewModel();
            model.Kids = db.Kids.Select(k => new KidViewModel
                {
                    KidId = k.KidId,
                    FirstName = k.FirstName,
                    LastName = k.LastName
                }).ToList();

            return View(model);
        }

        public ActionResult EditKid(int id)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            KidViewModel model = new KidViewModel();
            Kid kid = db.Kids.FirstOrDefault(k => k.KidId == id);

            model.FirstName = kid.FirstName;
            model.LastName = kid.LastName;
            model.KidId = kid.KidId;

            return View(model);
        }

        [HttpPost]
        public ActionResult EditKid(KidViewModel model, int id)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            Kid kid = db.Kids.FirstOrDefault(k => k.KidId == id);

            kid.FirstName = model.FirstName;
            kid.LastName = model.LastName;

            db.SaveChanges();
            return RedirectToAction("Kids");
        }
        public ActionResult CreateKid()
        {
            KidViewModel model = new KidViewModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult CreateKid(KidViewModel model)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            db.Kids.AddOrUpdate(k => new {k.FirstName, k.LastName},
                new Kid
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName
                }
                );

            db.SaveChanges();
            return RedirectToAction("Kids");
        }


        public ActionResult Chores()
        {
            KidChoreDbContext db = new KidChoreDbContext();
            ChoresViewModel model = new ChoresViewModel();

            model.Chores = db.Chores.Select(c => new ChoreViewModel 
            { 
            Description = c.Description,
            ChoreId = c.ChoreId
            }).ToList();

            return View(model);
        }

        public ActionResult UpdateChore(int id)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            ChoreViewModel model = new ChoreViewModel();
            Chore chore = db.Chores.FirstOrDefault(c => c.ChoreId == id);

            model.ChoreId = chore.ChoreId;
            model.Description = chore.Description;

            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateChore(ChoreViewModel model, int id)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            Chore chore = db.Chores.FirstOrDefault(c => c.ChoreId == id);

            chore.Description = model.Description;

            db.SaveChanges();
            
            return RedirectToAction("Chores");
        }

        public ActionResult CreateChore()
        {
            ChoreViewModel model = new ChoreViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult CreateChore(ChoreViewModel model)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            db.Chores.AddOrUpdate(c => new { c.Description },
                new Chore
                {
                    Description = model.Description
                }
                );

            db.SaveChanges();

            return RedirectToAction("Chores");
        }



        public ActionResult AssignChore()
        {
            KidChoreDbContext db = new KidChoreDbContext();
            KidsChoresListViewModel model = new KidsChoresListViewModel();
            model.Kids = db.Kids.Select(k => new KidViewModel 
            {
                KidId = k.KidId,
                FirstName = k.FirstName,
                LastName = k.LastName
            }).ToList();

            model.Chores = db.Chores.Select(c => new ChoreViewModel
            {
                ChoreId = c.ChoreId,
                Description = c.Description
            }).ToList();

            return View(model);
        }
        [HttpPost]
  //      public ActionResult AssignChore(KidChoreViewModel model)
        public ActionResult AssignChore(int kidId, int choreId)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            db.KidChores.AddOrUpdate(kc => new { kc.KidId, kc.ChoreId },
                new KidChore
                {
                    KidId = kidId,
                    ChoreId = choreId
                }
                );
            db.SaveChanges();

            return RedirectToAction("Index");
        }


        public ActionResult Delete(int id)
        {
            KidChoreDbContext db = new KidChoreDbContext();
            KidChore kidChore = db.KidChores.FirstOrDefault(kc => kc.KidChoreId == id);

            db.KidChores.Remove(kidChore);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        public ActionResult Index()
        {
            KidChoreDbContext db = new KidChoreDbContext();
            KidChoresViewModel model = new KidChoresViewModel();

            model.KidChores = db.KidChores.Select(kc => new KidChoreViewModel
            {
                KidChoreId = kc.KidChoreId,
                Kid = new KidViewModel
                {
                    KidId = kc.Kid.KidId,
                    FirstName = kc.Kid.FirstName,
                    LastName = kc.Kid.LastName
                },
               Chore = new ChoreViewModel
                {
                    ChoreId = kc.Chore.ChoreId,
                    Description = kc.Chore.Description
                }
            }).ToList();

  

            return View(model);
        }

 
    }
}